﻿using Microsoft.International.Converters.PinYinConverter;
using Newtonsoft.Json;
using WebHome_Admin.Models;
using WebHome_API.Models;
using WebHome_API.Models.DBModels;

namespace WebHome_Admin
{
    public static class Novel
    {
        public static void AddDataJson()
        {
            var path = @"C:\Users\41280\Desktop\novelData.json";
            List<Novel_Table> list = new();
            using (StreamReader sr = File.OpenText(path))
            {
                while (sr.Peek() != -1)
                {
                    //读取到的数据
                    string a = sr.ReadToEnd();
                    list = JsonConvert.DeserializeObject<List<Novel_Table>>(a);
                }
            }

            using (MyContext context = new())
            {
                foreach (var item in list)
                {
                    if (context.Novels.FirstOrDefault(u => u.Name == item.Name) == null)
                    {
                        context.Novels.Add(new Novel_Table()
                        {
                            Name = item.Name,
                            Category = item.BackImage,
                            FirstE = item.FirstE,
                            BackImage = "",
                            IsDel = 0,
                            LastUpdateTime = DateTime.Now,
                            UserId = 0,
                            DownloadCount = 0,
                        });
                    }
                }
                context.SaveChanges();
            }
        }

        public static void AddFileJson()
        {
            var path = @"C:\Users\41280\Desktop\novelFiles.json";
            List<FilePath2> list = new List<FilePath2>();
            using (StreamReader sr = File.OpenText(path))
            {
                while (sr.Peek() != -1)
                {
                    //读取到的数据
                    string a = sr.ReadToEnd();
                    list = JsonConvert.DeserializeObject<List<FilePath2>>(a);
                }
            }

            using (MyContext context = new())
            {
                foreach (var item in list)
                {
                    var novel = context.Novels.FirstOrDefault(u => u.Name == item.Name2);
                    if (novel == null)
                    {
                        Console.WriteLine(novel.Name);
                    }
                    else
                    {
                        context.FilePaths.Add(new FilePath_Table()
                        {
                            FileType = (int)FileTypeEnum.Novel,
                            FileId = novel.ID,
                            Name = item.Name2,
                            Path = item.Path,
                            UserId = 0,
                            CreateTime = DateTime.Now,
                            IsDel = 0,
                            DelTime = new DateTime(2001, 06, 04),
                        });
                    }
                }
                context.SaveChanges();
            }
        }

        public static void CreateDataJson()
        {
            List<Novel_Table> list = new List<Novel_Table>();

            var paths = Directory.GetDirectories("D:\\Novel");
            foreach (var path in paths)
            {
                var arr = path.Split('\\');
                var file = Directory.GetFiles(path);
                var category = "";
                foreach (var item in file)
                {
                    var temp = item.Substring(item.LastIndexOf('.') + 1, 3);
                    if (temp == "txt")
                    {
                        category = Path.GetFileNameWithoutExtension(item);
                        break;
                    }
                }

                if (arr[arr.Length - 1] != "P站小说" && arr[arr.Length - 1] != "龙族")
                {
                    list.Add(new Novel_Table()
                    {
                        Name = arr[arr.Length - 1],
                        Category = category,
                        FirstE = GetFirstPinyin(arr[arr.Length - 1]),
                        BackImage = "",
                        DownloadCount = 0,
                        IsDel = 0,
                        UserId = 0,
                        LastUpdateTime = DateTime.Now,
                    });
                }
            }

            string json = JsonConvert.SerializeObject(list);
            using (StreamWriter sw = new StreamWriter(@"C:\Users\SanYing\Desktop\novelData.json", true))
            {
                sw.WriteLine(json);
            }
        }

        public static void CreateFileJson()
        {
            var path = @"C:\Users\41280\Desktop\novelData.json";
            List<Novel_Table> list = new();
            using (StreamReader sr = File.OpenText(path))
            {
                while (sr.Peek() != -1)
                {
                    //读取到的数据
                    string a = sr.ReadToEnd();
                    list = JsonConvert.DeserializeObject<List<Novel_Table>>(a);
                }
            }

            List<FilePath2> filePaths = new List<FilePath2>();
            var path1 = "D:\\Novel\\";
            foreach (var item in list)
            {
                var paths = getFiles1(path1 + item.Name);
                foreach (var cur in paths)
                {
                    if (Path.GetExtension(cur) == ".epub")
                    {
                        var a = Path.GetFileName(cur);
                        var c = cur.Split('\\')[2];

                        filePaths.Add(new FilePath2()
                        {
                            Name = a,
                            Path = cur.Replace("D:\\Novel", ""),
                            Name2 = c,
                        });
                    }
                }
            }

            var json = JsonConvert.SerializeObject(filePaths);
            using (StreamWriter sw = new StreamWriter(@"C:\Users\SanYing\Desktop\novelFiles.json", true))
            {
                sw.Write(json);
            }
        }





        static List<string> getFiles1(string path)
        {
            List<string> list = new();
            var paths = Directory.GetDirectories(path);
            if (paths.Length > 0)
            {
                foreach (var item in paths)
                {
                    var cur = getFiles1(item);
                    foreach (var file in cur)
                    {
                        list.Add(file);
                    }
                }
            }
            //没有文件夹了读取文件
            var files = Directory.GetFiles(path);
            foreach (var file in files)
            {
                list.Add(file);
            }
            return list;
        }

        static string GetFirstPinyin(string str)
        {
            string r = string.Empty;
            var obj = str[0];
            try
            {
                ChineseChar chineseChar = new ChineseChar(obj);
                string t = chineseChar.Pinyins[0].ToString();
                r += t.Substring(0, 1);
            }
            catch
            {
                r += obj.ToString();
            }
            return r;
        }
    }
}
